const express = require('express')//库
const app = express()
const path = require('path')
var favicon = require('serve-favicon')
app.use(express.urlencoded({extended:false}))
app.listen(3000, () => {
    console.log('端口3000运行成功')
})
// url为login 进行渲染页面
app.get('/login', (req, res) => {
    res.sendFile(path.join(__dirname, '/pages/loginAndRegister/login.html'))
})
app.get('/register', (req, res) => {
    res.sendFile(path.join(__dirname, '/pages/loginAndRegister/register.html'))
})
// app.use('/css',express.static('css'))
app.use('/img',express.static('img'))
app.use('/css',express.static('css'))
app.use(favicon(path.join(__dirname, 'favicon.ico')))
// app.get('/111', (req, res) => {
//     res.sendFile(path.join(__dirname, '/public/1.html'))
// })

// 页面提交后为gologin 进行数据判断
const fs = require('fs')
// const { parse } = require('path')
let arr = fs.readFileSync('persons.json', 'utf-8')
arr = JSON.parse(arr)
app.post('/dologin', (req, res) => {
    username = req.body.tel;
    password = req.body.pass;
    // console.log(username)
    // console.log(password)
    let a = arr.find(item => username === item.username && password === item.password)
    if (a) {
        res.send('登录成功')
    } else {
        res.send('用户名密码错误')
    }
})
app.post('/doregister', (req, res) => {
    username = req.body.tel;
    password = req.body.pass;
    let a = arr.find(item => username === item.username)
    if (a) {
        res.send('用户名已被注册')
    } else {
        // let lujing = path.join(__dirname, 'persons.json')
        let w={
            username:req.body.tel,
            password:req.body.pass
        }
        arr.push(w)
        fs.writeFileSync(path.join(__dirname,'persons.json'),JSON.stringify(arr))
        res.send('注册成功')
    }
})

app.use((req,res)=>{
    res.status(404)
    res.send("<img style='width:200px heigth:100px' src='https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2F50%2Fv2-955d0bc97cac7cdefb323eae051d0e6e_hd.jpg&refer=http%3A%2F%2Fpic1.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1643378917&t=7c500f8b3f3c9230a416546e044f8e61'></img>")
})